home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
The World of Computer Software.iso
/
tcsel003.zip
/
SORTDEMO.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1992-10-16
|
2KB
|
76 lines
unit qsort;
interface
procedure quicksort(var s; left,right : word);
implementation
procedure quicksort(var s; left,right : word; SortType: stype);
{ On the first call left should always be = to min and right = to max }
var
data : DataArr absolute s;
pivotStr,
tempStr : string;
pivotLong,
tempLong : longint
lower,
upper,
middle : word;
procedure swap(var a,b);
var x : DirRec absolute a;
y : DirRec absolute b;
t : DirRec;
begin
t := x;
x := y;
y := t;
end;
begin
lower := left;
upper := right;
middle:= (left + right) div 2;
case SortType of
_name: pivotStr := data[middle].name;
_ext : pivotStr := data[middle].ext;
_size: pivotLong := data[middle].Lsize;
_date: pivotLong := data[middle].Ldate;
end; { case SortType }
repeat
case SortType of
_name: begin
while data[lower].name < pivotStr do inc(lower);
while pivotStr < data[upper].name do dec(upper);
end;
_ext : begin
while data[lower].ext < pivotStr do inc(lower);
while pivotStr < data[upper].ext do dec(upper);
end;
_size: begin
while data[lower].Lsize < pivotLong do inc(lower);
while pivotLong < data[upper].Lsize do dec(upper);
end;
_date: begin
while data[lower].Ldate < pivotLong do inc(lower);
while pivotLong < data[upper].Ldate do dec(upper);
end;
end; { case SortType }
if lower <= upper then begin
swap(data[lower],data[upper]);
inc(lower);
dec(upper);
end;
until lower > upper;
if left < upper then quicksort(data,left,upper);
if lower < right then quicksort(data,lower,right);
end; { quicksort }